package club.justwill.single.persistence.support;


import club.justwill.single.helper.CoreHelper;
import club.justwill.single.model.SecurityToken;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;
import java.io.Serializable;
import java.sql.Timestamp;

/**
 * Created by xiangnan on 10/28/15.
 */
@MappedSuperclass
public abstract class Jpo implements Serializable {

	@Column(name = "proc_name")
	private String processName;

	@Column(name = "proc_channel")
	private String processChannel;

	@Column(name = "session_id")
	private String sessionId;

	@Column(name = "proc_ts")
	public Timestamp processTimeStamp;

	@Column(name = "proc_user_id")
	private long processUserId;

	@Version
	private int version;


	protected void copyJpo(Jpo other) {
		setProcessName(other.getProcessName());
		setProcessChannel(other.getProcessChannel());
		setSessionId(other.getSessionId());
		setProcessTimeStamp(other.getProcessTimeStamp());
		setProcessUserId(other.getProcessUserId());
		setVersion(other.getVersion());
	}

	public void updateAudit(SecurityToken token) {
		setProcessName(token.getProcessName());
		setProcessChannel(CoreHelper.getServiceChannel(token.getChannel()));
		setProcessUserId(token.getOperatorUserId());
		setProcessTimeStamp(new Timestamp(token.getRequestTime().getTime()));
		setSessionId(token.getSessionId());
	}


	public String getProcessName() {
		return processName;
	}

	public void setProcessName(String processName) {
		this.processName = processName;
	}

	public String getProcessChannel() {
		return processChannel;
	}

	public void setProcessChannel(String processChannel) {
		this.processChannel = processChannel;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public Timestamp getProcessTimeStamp() {
		return processTimeStamp;
	}

	public void setProcessTimeStamp(Timestamp processTimeStamp) {
		this.processTimeStamp = processTimeStamp;
	}

	public long getProcessUserId() {
		return processUserId;
	}

	public void setProcessUserId(long processUserId) {
		this.processUserId = processUserId;
	}

	public int getVersion() {
		return version;
	}

	public void setVersion(int version) {
		this.version = version;
	}
}
